package com.royal.lr.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CountCharFrequency {

    /**
     * 统计单个字符出现的频率
     * @param str
     * @return
     */
    public static List<Map<String,Integer>> countSingChar(String str){
        char[] singChars=new char[50000];
        for(int i=0;i<str.length();i++){
            int charAscVal=str.charAt(i);
            singChars[charAscVal]= (char) (singChars[charAscVal]+1);
        }
        List<Map<String,Integer>> res=new ArrayList<>();
        for(int i=0;i<singChars.length;i++){
           char c= (char) i;
            int charCount=singChars[i];;
            if(charCount!=0){
                Map<String,Integer> charCountMap=new HashMap<>();
                charCountMap.put(String.valueOf(c),charCount);
            }
        }
        return res;
    }
    public static Map<Character,Integer> countSingChar2(String str){
        Map<Character,Integer> res=new HashMap<>();
        for(int i=0;i<str.length();i++){
            char c=str.charAt(i);
            if(res.containsKey(c)){
                res.put(c,res.get(c)+1);
            }else{
                res.put(c,1);
            }
        }
        return res;
    }
    /**
     * 统计单个字符连续出现的频率
     * @param str
     * @return
     */
    public static String countContinuityChar(String str){
        StringBuffer result = new StringBuffer();
        result.append(str.charAt(0));
        int c = 1;

        for (int i = 0; i < str.length() - 1; i++) {
            if (str.charAt(i) == str.charAt(i + 1)) {
                c++;
            } else {
                result.append(c);
                result.append(str.charAt(i+1));
                c = 1;
            }
        }
        result.append(c);
        return result.toString();
    }
}
